package com.rjj.wlqq.tool.console;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

/**
 * @author renjianjun
 * @date 2020/9/8 08:46
 * @email 215060580@qq.com
 *
 * 一个工具类【模仿jfinal的控制台输出】
 * 局限性：只可以跳到指定的类，不可以跳到指定的方法，因为获取不到方法所在的行数
 *
 * springboot controller report------2020-09-08 08:59:15 --------------------------
 * Url         : GET /pathTest
 * Controller  : com.rjj.wlqq.index.IndexController.(IndexController.java:1)
 * Method      : pathTest
 * --------------------------------------------------------------------------------
 *
 */
public class ConsoleTool {

    private static int maxOutputLengthOfParaValue = 512;
    private static Writer writer = new ConsoleTool.SystemOutWriter();


    private static ThreadLocal<SimpleDateFormat> sdf = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"));

    /**
     * @param request            请求
     * @param signatureName      实际controller中的请求的方法名称 pathTest
     * @param clazz              类的路径引用 com.rjj.wlqq.index.IndexController
     * @param requestMethod      请求方法的方式 get|post
     * @param requestServletPath 请求的路径 /pathTest
     * @param classStr           类名称String classStr = clazz.substring(clazz.lastIndexOf(".") + 1); ==   ((HandlerMethod) handler).getMethod().getDeclaringClass().getSimpleName()  => IndexController
     * @throws IOException
     */
    public static void Console(HttpServletRequest request, String signatureName, String clazz, String requestMethod, String requestServletPath, String classStr) throws IOException {
        String title = "springboot controller report------";
        StringBuilder sb = new StringBuilder(title).append(sdf.get().format(new Date())).append(" --------------------------\n");
        sb.append("Url         : ").append(requestMethod).append(" ").append(requestServletPath).append("\n");
        sb.append("Controller  : ").append(clazz).append(".(").append(classStr).append(".java:1)");
        sb.append("\nMethod      : ").append(signatureName).append("\n");

        Enumeration<String> e = request.getParameterNames();
        if (e.hasMoreElements()) {
            sb.append("Parameter   : ");

            for (; e.hasMoreElements(); sb.append("  ")) {
                String name = e.nextElement();
                String[] values = request.getParameterValues(name);
                if (values.length == 1) {
                    sb.append(name).append("=");
                    if (values[0] != null && values[0].length() > maxOutputLengthOfParaValue) {
                        sb.append(values[0], 0, maxOutputLengthOfParaValue).append("...");
                    } else {
                        sb.append(values[0]);
                    }
                } else {
                    sb.append(name).append("[]={");

                    for (int i = 0; i < values.length; ++i) {
                        if (i > 0) {
                            sb.append(",");
                        }

                        sb.append(values[i]);
                    }

                    sb.append("}");
                }
            }

            sb.append("\n");
        }

        sb.append("--------------------------------------------------------------------------------\n");

        writer.write(sb.toString());
    }

    private static class SystemOutWriter extends Writer {
        private SystemOutWriter() {
        }

        @Override
        public void write(String str) throws IOException {
            System.out.print(str);
        }

        @Override
        public void write(char[] cbuf, int off, int len) throws IOException {
        }

        @Override
        public void flush() throws IOException {
        }

        @Override
        public void close() throws IOException {
        }
    }
}
